package com.easy.dfcapi.service.impl;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.utils.StringUtil;
import com.easy.dfcapi.entity.ApiCart;
import com.easy.dfcapi.entity.ApiStore;
import com.easy.dfcapi.entity.ApiUser;
import com.easy.dfcapi.mapper.*;
import com.easy.dfcapi.res.CartRes;
import com.easy.dfcapi.res.CartRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.service.ApiCartService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import com.easy.modules.sys.entity.Para;
import com.easy.modules.sys.mapper.ParaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by VULCANTSeries on 2017/5/10.
 */
@Service
@Transactional(readOnly = true)
public class ApiCartServiceImpl implements ApiCartService {

    @Autowired
    private ApiUserMapper userMapper;

    @Autowired
    private ApiStoreMapper storeMapper;

    @Autowired
    private ApiCategoryMapper categoryMapper;

    @Autowired
    private ApiProductMapper productMapper;

    @Autowired
    private ApiCartMapper cartMapper;

    @Override
    @Transactional(readOnly = false)
    public Result CartAdd(Response<CartRes> response) throws Exception {
        Result rt = new Result();
        try {
            CartRes data = response.getData(CartRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }

            Date now = new Date();
            ApiCart cart = new ApiCart();
            //cart.setCartName(data.getCartName());
            //cart.setCartImg(data.getCartImg());
            //cart.setCartLogo(data.getCartLogo());
            //cart.setCartDesc(data.getCartDesc());
            //
            //cart.setStoreId(store.getStoreId());
            //cart.setAuditStatus(isAudit);
            cart.preInsert(now);
            if(cartMapper.insert(cart)>0){
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else {
                rt.setStatus(EnumConfig.S_CODE.FAIL);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result CartMod(Response<CartRes> response) throws Exception {
        Result rt = new Result();
        try {
            CartRes data = response.getData(CartRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiCart cart = cartMapper.get(data.getCartId());
            if(cart == null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }

            Date now = new Date();
            //cart.setCartName(data.getCartName());
            //cart.setCartImg(data.getCartImg());
            //cart.setCartLogo(data.getCartLogo());
            //cart.setCartDesc(data.getCartDesc());

            cart.preUpdate(now);
            cartMapper.update(cart);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    public Result CartList(Response<CartRes> response) throws Exception {
        Result rt = new Result();
        try {
            CartRes data = response.getData(CartRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            List<Map<String,Object>> cartList = cartMapper.getMapByUserId(user.getUserId());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(cartList);

        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result CartDel(Response<CartRes> response) throws Exception {
        Result rt = new Result();
        try {
            CartRes data = response.getData(CartRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiCart cart = cartMapper.get(data.getCartId());
            if(cart == null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }
            cartMapper.delete(cart);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

}
